# -*- coding: utf-8 -*-
from json import dumps
import hmac
import hashlib
from base64 import b64encode

import requests

# 钉钉通知
from config_loader import load_config
from sign.task_request import req

# 加载配置
cfg = load_config("config.yml")
PUSH_PLUS_TOKEN = cfg["PUSH_PLUS_TOKEN"]


# PushPlus推送模板
async def pushPlusAlert(**kwargs):
    access_token = kwargs.get("access_token", "")
    title = kwargs.get("title", "")
    content = kwargs.get("msg", "")
    secret = PUSH_PLUS_TOKEN
    if PUSH_PLUS_TOKEN:
        meta = {
            "method": "POST",
            "url": "http://www.pushplus.plus/send",
            "data": dumps({
                'token': PUSH_PLUS_TOKEN,
                'title': title,
                'content': content,
            }),
            "headers": {"Content-Type": "application/json"}
        }
        res = await req(**meta)
        if res and not res.json().get('errcode'):
            return True
        else:
            return False


def pushPlusNotify(title, content):
    title = title
    url = 'http://www.pushplus.plus/send'
    data = {
        "token": PUSH_PLUS_TOKEN,
        "title": title,
        "content": content
    }
    result = requests.post(url, data=data)
    return result

